package com.neusoft.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.neusoft.po.Credit;

@Mapper
public interface CreditMapper {
	// get total credit by userId and expired=0 and used=0
	@Select("select * from credit where userId=#{userId} and expired=0 and used=0")
	public List<Credit> listAvailableCreditByUserId(String userId);

	// update credit
	@Update("update credit set expired=#{expired},used=#{used},num=#{num},expiredTime=#{expiredTime} where creditId=#{creditId}")
	public int updateCredit(Credit credit);

	// get all credit by userId
	@Select("select * from credit where userId=#{userId}")
	public List<Credit> listAllCreditByUserId(String userId);

	// get credit by userId and channelType
	@Select("select * from credit where userId=#{userId} and channelType=#{channelType}")
	public List<Credit> listCreditByUserIdAndChannelType(String userId, int channelType);

	// insert credit
	@Insert("insert into credit(userId, num, channelType, createTime,expiredTime,expired,used) values(#{userId}, #{num}, #{channelType}, #{createTime},#{expiredTime},#{expired},#{used})")
	@Options(useGeneratedKeys = true, keyProperty = "creditId", keyColumn = "creditId")
	public int insertCredit(Credit credit);

}
